/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Actions;

import Randomizer.Randomizer;
import Users.Manager;
import com.opensymphony.xwork2.ActionSupport;
import db.DBConnector;
import java.security.MessageDigest;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Gabs
 */
public class AddManager extends ActionSupport {

    private Manager manager;
    private DBConnector db = null;
    private CallableStatement callable = null;

    @Override
    public String execute() throws Exception {
        PreparedStatement pstmt = null;
        Randomizer rand = new Randomizer();
        int adminID = rand.GenerateNumber();

        db = new DBConnector();
        Connection conn = null;

        try {
            conn = db.getConnection();

            MessageDigest md5 = MessageDigest.getInstance("SHA-256");
            md5.update(getManager().getPassword().getBytes());

            byte byteData1[] = md5.digest();

            StringBuffer stringbuf = new StringBuffer();

            for (int c = 0; c < byteData1.length; c++) {
                stringbuf.append(Integer.toString((byteData1[c] & 0xff) + 0x100, 16).substring(1));
            }

            String createCall = "{call insert_admin(?,?,?,?,?)}";
//---------- calls the insert customer procedure--------------------------------            
            callable = conn.prepareCall(createCall);
            callable.setInt(1, adminID);
            callable.setString(2, getManager().getUsername());
            callable.setString(3, stringbuf.toString());
            callable.setInt(4, getManager().getType());
            callable.setBoolean(5, true);


            callable.executeUpdate();
            conn.close();

            return SUCCESS;
        } catch (Exception e) {
            Logger.getLogger(AddManager.class.getName()).log(Level.SEVERE, null, e);
            return INPUT;
        }

    }

    /**
     * @return the manager
     */
    public Manager getManager() {
        return manager;
    }

    /**
     * @param manager the manager to set
     */
    public void setManager(Manager manager) {
        this.manager = manager;
    }
}